﻿ 
 
namespace ChessKit.Logic
{
  partial class ProtoBoard
  {
    private bool CanBeValidMove(Piece piece, int fromSquare, int toSquare)
    {
	    var dx = toSquare - fromSquare;
		if (dx < 0)
		{
	    switch (piece)
      {
        #region ' White Bishop '
        case Piece.WhiteBishop:
          if (dx % 17 == 0) 
          {
              for (var i = fromSquare - 17; i > toSquare; i -= 17)
                if (Squares[i] != 0) return false;
			  return true;
          }
          if (dx % 15 == 0) 
          {
              for (var i = fromSquare - 15; i > toSquare; i -= 15)
                if (Squares[i] != 0) return false;
			  return true;
          }
          return false;
        #endregion

        #region ' White Knight '
        case Piece.WhiteKnight:
          switch (dx)
          {      
            case -31:
              return true;
            case -33:
              return true;
            case -14:
              return true;
            case -18:
              return true;
          }
	      return false;
        #endregion

        #region ' White Rook '
        case Piece.WhiteRook:
          if (dx % 16 == 0) 
          {
              for (var i = fromSquare - 16; i > toSquare; i -= 16)
                if (Squares[i] != 0) return false;
			  return true;
          }
          if (dx > -8) 
          {
              for (var i = fromSquare - 1; i > toSquare; i -= 1)
                if (Squares[i] != 0) return false;
			  return true;
          }
          return false;
        #endregion

        #region ' White Queen '
        case Piece.WhiteQueen:
          if (dx % 17 == 0) 
          {
              for (var i = fromSquare - 17; i > toSquare; i -= 17)
                if (Squares[i] != 0) return false;
			  return true;
          }
          if (dx % 16 == 0) 
          {
              for (var i = fromSquare - 16; i > toSquare; i -= 16)
                if (Squares[i] != 0) return false;
			  return true;
          }
          if (dx % 15 == 0) 
          {
              for (var i = fromSquare - 15; i > toSquare; i -= 15)
                if (Squares[i] != 0) return false;
			  return true;
          }
          if (dx > -8) 
          {
              for (var i = fromSquare - 1; i > toSquare; i -= 1)
                if (Squares[i] != 0) return false;
			  return true;
          }
          return false;
        #endregion

        #region ' White King '
        case Piece.WhiteKing:
          switch (dx)
          {      
            case -15:
              return true;
            case -16:
              return true;
            case -17:
              return true;
            case -1:
              return true;
            case -2:
              return Squares[toSquare] == 0;
          }
	      return false;
        #endregion

        #region ' Black Pawn '
        case Piece.BlackPawn:
          switch (dx)
          {      
            case -16:
              return Squares[toSquare] == 0;
            case -15:
              return true;
            case -17:
              return true;
            case -32:
              return Squares[toSquare] == 0;
          }
	      return false;
        #endregion

        #region ' Black Bishop '
        case Piece.BlackBishop:
          if (dx % 17 == 0) 
          {
              for (var i = fromSquare - 17; i > toSquare; i -= 17)
                if (Squares[i] != 0) return false;
			  return true;
          }
          if (dx % 15 == 0) 
          {
              for (var i = fromSquare - 15; i > toSquare; i -= 15)
                if (Squares[i] != 0) return false;
			  return true;
          }
          return false;
        #endregion

        #region ' Black Knight '
        case Piece.BlackKnight:
          switch (dx)
          {      
            case -31:
              return true;
            case -33:
              return true;
            case -14:
              return true;
            case -18:
              return true;
          }
	      return false;
        #endregion

        #region ' Black Rook '
        case Piece.BlackRook:
          if (dx % 16 == 0) 
          {
              for (var i = fromSquare - 16; i > toSquare; i -= 16)
                if (Squares[i] != 0) return false;
			  return true;
          }
          if (dx > -8) 
          {
              for (var i = fromSquare - 1; i > toSquare; i -= 1)
                if (Squares[i] != 0) return false;
			  return true;
          }
          return false;
        #endregion

        #region ' Black Queen '
        case Piece.BlackQueen:
          if (dx % 17 == 0) 
          {
              for (var i = fromSquare - 17; i > toSquare; i -= 17)
                if (Squares[i] != 0) return false;
			  return true;
          }
          if (dx % 16 == 0) 
          {
              for (var i = fromSquare - 16; i > toSquare; i -= 16)
                if (Squares[i] != 0) return false;
			  return true;
          }
          if (dx % 15 == 0) 
          {
              for (var i = fromSquare - 15; i > toSquare; i -= 15)
                if (Squares[i] != 0) return false;
			  return true;
          }
          if (dx > -8) 
          {
              for (var i = fromSquare - 1; i > toSquare; i -= 1)
                if (Squares[i] != 0) return false;
			  return true;
          }
          return false;
        #endregion

        #region ' Black King '
        case Piece.BlackKing:
          switch (dx)
          {      
            case -15:
              return true;
            case -16:
              return true;
            case -17:
              return true;
            case -1:
              return true;
            case -2:
              return Squares[toSquare] == 0;
          }
	      return false;
        #endregion

        default: return false;
	    }
		}
		// -----------------------------------------------------------------
		// else if (dx >= 0)
	    switch (piece)
      {
        #region ' White Pawn '
        case Piece.WhitePawn:
          switch (dx)
          {      
            case 16:
              return Squares[toSquare] == 0;
            case 17:
              return true;
            case 15:
              return true;
            case 32:
              return Squares[toSquare] == 0;
          }
	      return false;
        #endregion

        #region ' White Bishop '
        case Piece.WhiteBishop:
          if (dx % 17 == 0) 
          {
              for (var i = fromSquare + 17; i < toSquare; i += 17)
                if (Squares[i] != 0) return false;
			  return true;
          }
          if (dx % 15 == 0) 
          {
              for (var i = fromSquare + 15; i < toSquare; i += 15)
                if (Squares[i] != 0) return false;
			  return true;
          }
          return false;
        #endregion

        #region ' White Knight '
        case Piece.WhiteKnight:
          switch (dx)
          {      
            case 33:
              return true;
            case 31:
              return true;
            case 18:
              return true;
            case 14:
              return true;
          }
	      return false;
        #endregion

        #region ' White Rook '
        case Piece.WhiteRook:
          if (dx % 16 == 0) 
          {
              for (var i = fromSquare + 16; i < toSquare; i += 16)
                if (Squares[i] != 0) return false;
			  return true;
          }
          if (dx < 8) 
          {
              for (var i = fromSquare + 1; i < toSquare; i += 1)
                if (Squares[i] != 0) return false;
			  return true;
          }
          return false;
        #endregion

        #region ' White Queen '
        case Piece.WhiteQueen:
          if (dx % 17 == 0) 
          {
              for (var i = fromSquare + 17; i < toSquare; i += 17)
                if (Squares[i] != 0) return false;
			  return true;
          }
          if (dx % 16 == 0) 
          {
              for (var i = fromSquare + 16; i < toSquare; i += 16)
                if (Squares[i] != 0) return false;
			  return true;
          }
          if (dx % 15 == 0) 
          {
              for (var i = fromSquare + 15; i < toSquare; i += 15)
                if (Squares[i] != 0) return false;
			  return true;
          }
          if (dx < 8) 
          {
              for (var i = fromSquare + 1; i < toSquare; i += 1)
                if (Squares[i] != 0) return false;
			  return true;
          }
          return false;
        #endregion

        #region ' White King '
        case Piece.WhiteKing:
          switch (dx)
          {      
            case 16:
              return true;
            case 17:
              return true;
            case 1:
              return true;
            case 15:
              return true;
            case 2:
              return Squares[toSquare] == 0;
          }
	      return false;
        #endregion

        #region ' Black Bishop '
        case Piece.BlackBishop:
          if (dx % 17 == 0) 
          {
              for (var i = fromSquare + 17; i < toSquare; i += 17)
                if (Squares[i] != 0) return false;
			  return true;
          }
          if (dx % 15 == 0) 
          {
              for (var i = fromSquare + 15; i < toSquare; i += 15)
                if (Squares[i] != 0) return false;
			  return true;
          }
          return false;
        #endregion

        #region ' Black Knight '
        case Piece.BlackKnight:
          switch (dx)
          {      
            case 33:
              return true;
            case 31:
              return true;
            case 18:
              return true;
            case 14:
              return true;
          }
	      return false;
        #endregion

        #region ' Black Rook '
        case Piece.BlackRook:
          if (dx % 16 == 0) 
          {
              for (var i = fromSquare + 16; i < toSquare; i += 16)
                if (Squares[i] != 0) return false;
			  return true;
          }
          if (dx < 8) 
          {
              for (var i = fromSquare + 1; i < toSquare; i += 1)
                if (Squares[i] != 0) return false;
			  return true;
          }
          return false;
        #endregion

        #region ' Black Queen '
        case Piece.BlackQueen:
          if (dx % 17 == 0) 
          {
              for (var i = fromSquare + 17; i < toSquare; i += 17)
                if (Squares[i] != 0) return false;
			  return true;
          }
          if (dx % 16 == 0) 
          {
              for (var i = fromSquare + 16; i < toSquare; i += 16)
                if (Squares[i] != 0) return false;
			  return true;
          }
          if (dx % 15 == 0) 
          {
              for (var i = fromSquare + 15; i < toSquare; i += 15)
                if (Squares[i] != 0) return false;
			  return true;
          }
          if (dx < 8) 
          {
              for (var i = fromSquare + 1; i < toSquare; i += 1)
                if (Squares[i] != 0) return false;
			  return true;
          }
          return false;
        #endregion

        #region ' Black King '
        case Piece.BlackKing:
          switch (dx)
          {      
            case 16:
              return true;
            case 17:
              return true;
            case 1:
              return true;
            case 15:
              return true;
            case 2:
              return Squares[toSquare] == 0;
          }
	      return false;
        #endregion

        default: return false;
	    }
    }
  }
}